package com.tan.core.response;

import com.tan.core.enums.ErrorEnum;
import lombok.Data;

/**
 * @author Tan
 * @description
 * @date 2025/2/6
 */
@Data
public class ApiResponse<T> {
    private T data;

    private int code;

    private String msg;

    public ApiResponse<T> code(int code) {
        this.code = code;
        return this;
    }

    public ApiResponse<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public ApiResponse<T> data(T data) {
        this.data = data;
        return this;
    }

    public static <T> ApiResponse<T> create(int code, String msg, T data) {
        return new ApiResponse<T>().code(code).msg(msg).data(data);
    }

    private static <T> ApiResponse<T> create(ErrorEnum errorEnum, T data) {
        return create(errorEnum.getCode(), errorEnum.getMsg(), data);
    }


    public static <T> ApiResponse<T> success() {
        return create(ErrorEnum.SUCCESS, null);
    }

    public static <T> ApiResponse<T> success(T data) {
        return create(ErrorEnum.SUCCESS, data);
    }


    public static <T> ApiResponse<T> error() {
        return create(ErrorEnum.SERVER_ERROR, null);
    }


    public static <T> ApiResponse<T> error(ErrorEnum errorEnum) {
        return create(errorEnum, null);
    }

}
